import 'dart:io';

import 'package:base_flutter/base_flutter.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/a_mine/page/sos_setting_page.dart';
import 'package:ls_work/common/common.dart';
import 'package:ls_work/config/global_configs.dart';
import 'package:ls_work/init_page/webview_page.dart';

// import 'package:ls_work/login/login_router.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/styles.dart';
import 'package:ls_work/ui/permission_manage_page.dart';
import 'package:ls_work/update_app_utils/app_update_utils.dart';
import 'package:ls_work/update_app_utils/update_dialog.dart';
import 'package:ls_work/util/toast.dart';
import 'package:ls_work/widgets/click_item.dart';
import 'package:ls_work/a_mine/my_router.dart';
import 'package:ls_work/routers/fluro_navigator.dart';
import 'package:ls_work/a_mine/widget/exit_dialog.dart';

import '../../login/login_router.dart';
import '../../login/page/user_manager.dart';
import '../../net/api/http_api.dart';
import '../../service/sos_warning_service.dart';
import '../../ui/waring/set_warning_info_page.dart';
import '../../util/flat_button.dart';
import '../../util/request_url_utils.dart';

class MySettingPage extends StatefulWidget {
  @override
  _MySettingPageState createState() => _MySettingPageState();
}

class _MySettingPageState extends State<MySettingPage> {
  AppUpdateUtil? utils;

  void _checkUpdate() {
    if (Platform.isAndroid) {
      utils = new AppUpdateUtil();
      utils?.getVersion().then((value) {
        if (value != null) {
          _showUpdateDialog(value);
        } else {
          Toast.show('已是最新版本');
        }
      });
    } else {
      Toast.show('此APP仅支持安卓设备');
    }
  }

  void _showUpdateDialog(Map netMap) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (_) => UpdateDialog(netMap: netMap),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('设置').onTap(() {
          setState(() {
            DevConfig.instance.openDevModel(context);
          });
        },splashColor: Colors.transparent,highlightColor: Colors.transparent),
        elevation: 0.5,
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                children: <Widget>[
                  // ClickItem(
                  //   title: '消息设置',
                  //   titleStyle: TextStyles.textDark16,
                  //   onTap: () {
                  //     NavigatorUtils.push(context, MyRouter.messageSetting);
                  //   },
                  // ),
                  ClickItem(
                    title: '修改密码',
                    titleStyle: TextStyles.textDark16,
                    onTap: () {
                      NavigatorUtils.push(context, LoginRouter.resetPasswordPage);
                    },
                  ),
                  ClickItem(
                    title: '清理缓存',
                    titleStyle: TextStyles.textDark16,
                    onTap: () {
                      Toast.show('缓存清理成功');
                    },
                  ),
                  ClickItem(
                    title: '版本更新',
                    content:
                    '当前版本${SpUtil.getString(Constant.locaVersion) ?? ''}',
                    titleStyle: TextStyles.textDark16,
                    onTap: _checkUpdate,
                  ),

                  // IconTitleWidget(
                  //   '离线巡防',
                  //   showArrow: false,
                  //   contentWidget: Switch(
                  //     value: GlobalConfigs.useOfflinePatrol,
                  //     onChanged: (val) {
                  //       setState(() {
                  //         GlobalConfigs.saveUseOfflineSetting(val);
                  //       });
                  //     },
                  //   ),
                  // ),

                  // IconTitleWidget(
                  //   '显示管辖区域长者',
                  //   showArrow: false,
                  //   contentWidget: Switch(
                  //     value: GlobalConfigs.useArea,
                  //     onChanged: (val) {
                  //       setState(() {
                  //         GlobalConfigs.saveUseArea(val);
                  //       });
                  //     },
                  //   ),
                  // ),

                  ClickItem(
                    title: '报警设置',
                    titleStyle: TextStyles.textDark16,
                    onTap: () async {
                      Go().push(SosSettingPage());
                      // var data = await Go().push(SetWarningInfoPage());
                      // if(data!=null){
                      //   _setSosService();
                      // }
                    },
                  ),

                  if(DevConfig.instance.inDevModel)
                    IconTitleTextWidget(
                      '开发者模式',
                      showArrow: true,
                      arrow: Icon(Icons.keyboard_arrow_right_sharp),
                    ).onTap(() {
                      Go().push(DeveloperPage());
                    }),

                  // IconTitleWidget(
                  //   'nfc直接提交巡防',
                  //   showArrow: false,
                  //   contentWidget: Switch(
                  //     value: GlobalConfigs.nfcSubmitDirect,
                  //     onChanged: (val) {
                  //       setState(() {
                  //         GlobalConfigs.saveNfcSubmitDirect(val);
                  //       });
                  //     },
                  //   ),
                  // ),

                  // IconTitleWidget(
                  //   '不显示未到时间巡防任务',
                  //   showArrow: false,
                  //   contentWidget: Switch(
                  //     value: GlobalConfigs.filterPatrolTask,
                  //     onChanged: (val) {
                  //       setState(() {
                  //         GlobalConfigs.saveFilterPatrolTask(val);
                  //       });
                  //     },
                  //   ),
                  // ),

                  if(GlobalConfigs.needShowPolicy)
                    ...[
                      ClickItem(
                        title: '服务协议',
                        titleStyle: TextStyles.textDark16,
                        onTap: () async {
                          Go().push(WebViewPage(title: '服务协议', url: GlobalConfigs.agreenment,isLocal: false,));
                        },
                      ),

                      ClickItem(
                        title: '隐私政策摘要',
                        titleStyle: TextStyles.textDark16,
                        onTap: () async {
                          Go().push(WebViewPage(title: '隐私政策摘要', url: GlobalConfigs.privateSimple,isLocal: false,));
                        },
                      ),


                      ClickItem(
                        title: '隐私政策',
                        titleStyle: TextStyles.textDark16,
                        onTap: () async {
                          Go().push(WebViewPage(title: '隐私政策', url: GlobalConfigs.privacy,isLocal: false,));
                        },
                      ),

                      ClickItem(
                        title: '收集个人信息清单',
                        titleStyle: TextStyles.textDark16,
                        onTap: () async {
                          Go().push(WebViewPage(title: '收集个人信息清单', url: GlobalConfigs.personalInfoList,isLocal: false,));
                        },
                      ),

                      ClickItem(
                        title: '向第三方共享个人信息清单',
                        titleStyle: TextStyles.textDark16,
                        onTap: () async {
                          Go().push(WebViewPage(title: '向第三方共享个人信息清单', url: GlobalConfigs.sdkShare,isLocal: false,));
                        },
                      ),

                      ClickItem(
                        title: 'app权限使用清单',
                        titleStyle: TextStyles.textDark16,
                        onTap: () async {
                          Go().push(WebViewPage(title: 'app权限使用清单', url: GlobalConfigs.appPermissionList,isLocal: false,));
                        },
                      ),

                      ClickItem(
                        title: '系统权限管理',
                        titleStyle: TextStyles.textDark16,
                        onTap: () async {
                          Go().push(PermissionManagePage());

                        },
                      ),
                    ]

                ],
              ),
            ),
          ),
          Container(
            padding: EdgeInsets.only(left: 20, right: 20, bottom: 40),
            height: 90,
            width: double.infinity,
            child: FlatsButton(
              child: Text("退出登录",
                  style: TextStyle(fontSize: 18, color: Colours.text_normal)),
              color: Colours.bg_gray,
              onPressed: () {
                showDialog(
                  context: context,
                  barrierDismissible: false,
                  builder: (_) => ExitDialog(),
                );
              },
            ),
          ),
        ],
      ),
    );
  }

  Future<void> _setSosService() async {
    if (await SosWarningService.instance.startService()) {
      Future.delayed(Duration(milliseconds: 300), () {
        print("startService");
        SosWarningService.instance.setReceiveArea(
            GlobalConfigs.selectWarningType,
            GlobalConfigs.selectBuildingAndFloor);
        SosWarningService.instance.sendUrlMsg(
            HttpApi.baseUrl, RequestUrlUtils.branchId,
            accountName: UserManager.userName ?? "admin111");
        SosWarningService.instance.sendEnableReceive(GlobalConfigs.receiveSos);
      });
    }
  }

}
